/*
 * GPoint.java
 *
 * Created on den 30 november 2007, 19:53
 */

package grex.Graphics;

import grex.Data.Prediction;
import grex.Nodes.Node;
import grex.Nodes.If;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.NumberFormat;

import javax.swing.JOptionPane;

/**
 *
 * @author  RIK
 */
public class GPoint extends javax.swing.JPanel implements MouseListener{
    private double xValue=0,yValue=0,prob,lowerProb;    
    /** Creates new form GPoint */
    boolean realValue;
    private Node leaf;
    Prediction p;
    
    public GPoint(double xValue, double yValue,double prob,double lowerProb,Node leaf,boolean realValue) {
            initComponents();     
            this.leaf=leaf;
            this.xValue = xValue;
            this.yValue = yValue;   
            this.realValue=realValue;
            this.prob=prob;
            this.lowerProb=lowerProb;
            this.addMouseListener(this);       
            NumberFormat nf = NumberFormat.getInstance();
            nf.setMaximumFractionDigits(2);
            this.setToolTipText("X="+ nf.format(xValue) +  " Y="+nf.format(yValue));      
            if(realValue){
                jLabel1.setIcon(null);
                this.setForeground(Color.RED);
                jLabel1.setText("R");
                                
            }
    }
    public GPoint(double xValue, double yValue,double prob,double lowerProb,Node leaf,boolean realValue,Prediction p) {
        this(xValue, yValue, prob, lowerProb, leaf, realValue);
        this.p=p;
        jLabel1.setText("P");
        jLabel1.setIcon(null);
        setVisible(false);
    }
    public GPoint(double xValue, double yValue,Node leaf,boolean realValue,Prediction p) {
         this(xValue, yValue, leaf, realValue);
         this.p=p;
         jLabel1.setIcon(null);
         jLabel1.setText("P");
         setVisible(false);
     }
         
    public void highLigt(Boolean v){
       /// jLabel1.setForeground(Color.YELLOW);
        
        jLabel1.setVisible(v);
        
    }
            
     public GPoint(double xValue, double yValue,Node leaf,boolean realValue) {
            initComponents();     
            this.leaf=leaf;
            this.xValue = xValue;
            this.yValue = yValue;   
            this.realValue=realValue;
            this.prob=0;
            this.lowerProb=0;
            this.addMouseListener(this);       
            NumberFormat nf = NumberFormat.getInstance();
            nf.setMaximumFractionDigits(2);
            this.setToolTipText("X="+ nf.format(xValue) +  " Y="+nf.format(yValue));      
            if(realValue){
                jLabel1.setIcon(null);
                this.setForeground(Color.RED);
                jLabel1.setText("R");    
            }else{
                jLabel1.setIcon(null);
                this.setForeground(Color.BLUE);                
                jLabel1.setText("*");
            }
            yValue-=jLabel1.getHeight()/2;
            xValue-=jLabel1.getWidth()/2;
            jLabel1.setVisible(false);

          //      jLabel1.setIcon(null);
          //      jLabel1.setText("");
            
    }
     

     
     public Prediction getPrediction(){
         return p;
     }
  public void mouseClicked(MouseEvent e) {
      String s ="";
      if(!realValue){
          Node g = leaf;
          s+="--> "+g;
          while(g!=null && g!=g.getParent()){
              Node p=g.getParent();
              if(p instanceof If){
                  Node[] children = p.getChildren();
                  String condResult="";
                  if(g==children[2]){//Condition is false
                      condResult="!";
                  }
                  
                  s = condResult + getGeneAndChildrenAsString(children[0]) + " " + s;
                  g=p;
              }
          }
          s=  "G-REX Rule: " + s;
          JOptionPane.showMessageDialog(this.getParent(), s);
      }
    }
    
    private String getGeneAndChildrenAsString(Node gene){
        String condition="",left="",right="";
        Node[] children= gene.getChildren();
        if(children!=null){
            if(children.length==2){
                
            left = getGeneAndChildrenAsString(children[0]);
            right = getGeneAndChildrenAsString(children[1]);
            return "(" +left + " " + gene + " " + right+")"; 
            }else{//IF 3 children
              condition = getGeneAndChildrenAsString(children[0]);  
              left = getGeneAndChildrenAsString(children[1]);
              right = getGeneAndChildrenAsString(children[2]);
              return "if (" + condition +":" + left +":" + right + ")"; 
            }
        }else
            return gene.toString();
        
    }
    

    public void mousePressed(MouseEvent e) {
  //      throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent e) {
   //     throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
     //   throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet.");
    }

    public double getXValue() {
        return xValue;
    }

    public double getYValue() {
        return yValue;
    }    
    
    public double getProb(){
        return prob;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        setBackground(java.awt.Color.white);
        setLayout(new java.awt.BorderLayout());

        jLabel1.setBackground(java.awt.Color.white);
        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, 14));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/grex/Graphics/pics/G16.gif"))); // NOI18N
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jLabel1.setAlignmentY(0.0F);
        add(jLabel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    public double getLowerProb() {
        if(lowerProb == -1)
            return prob;
        return lowerProb;
    }

    public void setLowerProb(double lowerProb) {
        this.lowerProb = lowerProb;
    }
    
}
